/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrameAcessarSala.java
 *
 * Created on 30/05/2012, 21:15:21
 */

package br.edu.fatec.ads.view;

import br.edu.fatec.ads.classes.Acesso;
import br.edu.fatec.ads.classes.Cartao;
import br.edu.fatec.ads.classes.Sala;
import br.edu.fatec.ads.facade.FacadeAcesso;
import br.edu.fatec.ads.facade.FacadeCartao;
import br.edu.fatec.ads.util.FrameUtil;
import java.sql.Timestamp;

/**
 *
 * @author Ubirajara
 */
public class FrameAcessarSala extends javax.swing.JInternalFrame {

    private short secretaria_academica = 6;
    private short laboratorio_assembly = 7;
    private short laboratorio_sd = 8;

    /** Creates new form FrameAcessarSala */
    public FrameAcessarSala() {
        initComponents();

        FrameUtil.getInstance().removerBarraTitulo(this);

        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnAcessoSecretaria = new javax.swing.JButton();
        lblSecretaria = new javax.swing.JLabel();
        btnAcessoAssembly = new javax.swing.JButton();
        lblAssembly = new javax.swing.JLabel();
        btnAcessoSD = new javax.swing.JButton();
        lblAcessoSD = new javax.swing.JLabel();
        btnVoltar = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(800, 500));
        setPreferredSize(new java.awt.Dimension(800, 500));

        btnAcessoSecretaria.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/edu/fatec/ads/image/padlock_icon.png"))); // NOI18N
        btnAcessoSecretaria.setToolTipText("Entrar");
        btnAcessoSecretaria.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAcessoSecretariaActionPerformed(evt);
            }
        });

        lblSecretaria.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblSecretaria.setText("Laboratório de Assembly");

        btnAcessoAssembly.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/edu/fatec/ads/image/padlock_icon.png"))); // NOI18N
        btnAcessoAssembly.setToolTipText("Entrar");
        btnAcessoAssembly.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAcessoAssemblyActionPerformed(evt);
            }
        });

        lblAssembly.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblAssembly.setText("Secretaria Acadêmica");

        btnAcessoSD.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/edu/fatec/ads/image/padlock_icon.png"))); // NOI18N
        btnAcessoSD.setToolTipText("Entrar");
        btnAcessoSD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAcessoSDActionPerformed(evt);
            }
        });

        lblAcessoSD.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblAcessoSD.setText("Laboratório de S.D.");

        btnVoltar.setText("Voltar");
        btnVoltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVoltarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblAssembly)
                        .addGap(59, 59, 59)
                        .addComponent(lblSecretaria))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnVoltar)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnAcessoSecretaria, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(98, 98, 98)
                                .addComponent(btnAcessoAssembly, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAcessoSD)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btnAcessoSD, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(37, 37, 37))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(120, 120, 120)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblAcessoSD)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAcessoSD, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblAssembly)
                            .addComponent(lblSecretaria))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnAcessoAssembly, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnAcessoSecretaria, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(30, 30, 30)
                .addComponent(btnVoltar)
                .addContainerGap(148, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAcessoSecretariaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAcessoSecretariaActionPerformed
        this.tentativaAcesso(secretaria_academica);
    }//GEN-LAST:event_btnAcessoSecretariaActionPerformed

    private void btnAcessoAssemblyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAcessoAssemblyActionPerformed
        this.tentativaAcesso(laboratorio_assembly);
    }//GEN-LAST:event_btnAcessoAssemblyActionPerformed

    private void btnAcessoSDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAcessoSDActionPerformed
        this.tentativaAcesso(laboratorio_sd);
    }//GEN-LAST:event_btnAcessoSDActionPerformed

    private void btnVoltarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVoltarActionPerformed
        FrameUtil.getInstance().alterarTituloJanela(this, null);

        this.dispose();
    }//GEN-LAST:event_btnVoltarActionPerformed

    private void tentativaAcesso(short codigoSala){
        boolean validacao = false;
        Integer codigoCartao = null;

        Acesso acesso = null;
        Sala sala = new Sala();

        sala.setCodigo(codigoSala);

        try{

            codigoCartao = FacadeCartao.getInstance().recuperarCodigoCartaoAcesso();

            if(codigoCartao != null){

                validacao = FacadeCartao.getInstance().acessarSala(sala);

                if(validacao){
                    FrameUtil.getInstance().adicionarMensagemSucesso("Acesso permitido!");
                }else{
                    FrameUtil.getInstance().adicionarMensagemErro("Acesso negado!");
                }

                acesso = this.popularTentativaAcesso(codigoCartao, sala, validacao);

                FacadeAcesso.getInstance().inserirTentativaAcesso(acesso);

            }else{
                FrameUtil.getInstance().adicionarMensagemErro("Favor inserir o cartão na leitora para verificação!");
            }

        }catch(Exception cause){
            FrameUtil.getInstance().adicionarMensagemErro(cause.getMessage());
        }
    }

    private Acesso popularTentativaAcesso(int codigoCartao, Sala sala, boolean tentativaAcesso){

        Acesso acesso = new Acesso();
        Cartao cartao = new Cartao(codigoCartao);

        acesso.setCartao(cartao);
        acesso.setDataAcesso(new Timestamp(System.currentTimeMillis()));
        acesso.setSala(sala);
        acesso.setAutorizacaoEntrada(tentativaAcesso);

        return acesso;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAcessoAssembly;
    private javax.swing.JButton btnAcessoSD;
    private javax.swing.JButton btnAcessoSecretaria;
    private javax.swing.JButton btnVoltar;
    private javax.swing.JLabel lblAcessoSD;
    private javax.swing.JLabel lblAssembly;
    private javax.swing.JLabel lblSecretaria;
    // End of variables declaration//GEN-END:variables

}